#!/bin/bash
cd ~/app-root/data/temp/hosts
#iss
wget -t1 -l1 -np -nd --accept=html http://www.ishadowsocks.net/ --output-document=ss.html
if ! diff -q ss.html ssb.html; then
mv -f ss.html ssb.html
for i in A B C; do
grep -A 3 $i'服务器' ssb.html >ss$i.txt
domain=$(grep -o '[a-z0-9]\{1,5\}\.[a-z0-9]\{1,5\}\.[a-z0-9]\{1,5\}' ss$i.txt)
ip=$(ping $domain -c1 | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' |sort -u)
sed -i "s#$domain#$ip#" ss$i.txt
done
sed -i 's#.*服务器地址:\(.*\)<.*#{\n  \"server\": \"\1",#' ss*.txt
sed -i 's#.*端口:\(.*\)<.*#  \"server_port\": \1,#' ss*.txt
sed -i 's#.*密码:\(.*\)<.*#  \"password\": \"\1\",#' ss*.txt
sed -i 's#.*加密方式:\(.*\)<.*#  \"method\": \"\1\"\n}#' ss*.txt
sed -i '/password/a\\  "timeout\": 60,' ss*.txt
mv -f ss*.txt ~/app-root/runtime/repo/hosts/
#upload
cd ~/app-root/runtime/repo/hosts/
rm -f $OPENSHIFT_DATA_DIR/hosts/*.txt $OPENSHIFT_DATA_DIR/hosts/*.pac $OPENSHIFT_DATA_DIR/hosts/*.conf
cp -f *.txt $OPENSHIFT_DATA_DIR/hosts/
cp -f *.conf $OPENSHIFT_DATA_DIR/hosts/
cd $OPENSHIFT_DATA_DIR/hosts/
git add --all
git commit -m "update"
git push -u origin master
else
rm -f ss.html
fi
